<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Dashboard extends Controller {

    function __construct(){
        parent::Controller();
		$this->current_user = models\Current_user::get_user();
		if(!$this->current_user) 
			show_error(array('Halaman ini tidak bisa diakses oleh anda!',anchor('','Kembali ke halaman utama')),403);

		$this->sidebar_menu_links['index'] = 'Home';
		$this->sidebar_menu_links['ganti_password'] = 'Ganti Password';
		$this->sidebar_menu_links['ganti_email'] = 'Ganti Email';
		if(models\Current_user::previlege('Administrator')){
			//$this->sidebar_menu_links['email'] = 'Ganti Email Gateway';
			//$this->sidebar_menu_links['edit_paket_speedy'] = 'Edit Paket Speedy';
			$this->sidebar_menu_links['reset_user_password'] = 'Reset User Password';
			//$this->sidebar_menu_links['log'] = 'Log';
		}
    }

    function index(){
		$this->headbar->add_header('Dashboard');

		if(models\Current_user::previlege('Administrator')){
			$this->admin_dashboard();
		} else if(models\Current_user::previlege('Agen')){
			$this->admin_dashboard();
		} else if(models\Current_user::previlege('Teknisi')){
			$this->teknisi_dashboard();
		} else if(models\Current_user::previlege('Customer|Perusahaan')){
			$this->customer_dashboard();
		}

		$this->init_sidebar();
		$this->load->view('template', array('title' => 'Dashboard'));
    }

	function ganti_password(){
		$this->init_sidebar();
		$this->headbar->add_header('Ganti Password');
		$this->main_content->add_custom('p','Mengganti password Akun');
		$this->main_content->add_block()
			->add_password_field('password_lama','Password Lama')
			->add_password_field('password_baru','Password Baru')
			->add_password_field('password_confirm','Konfirmasi Password Baru')
			->set_form($this->uri->segment(1).'/ajax_ganti_password','ganti_password');
		$this->main_content->add_script('ganti_password');
		$this->load->view('template', array('title' => 'Configuration'));
	}

	function ajax_ganti_password(){
		if(IS_AJAX){
			$this->load->library('form_validation');
			if($this->form_validation->run('ganti_password') == TRUE){
				$akun = models\Current_user::get_user();
				$akun->set_password($this->input->post('password_baru'));
				$this->doctrine->em->flush();
				$returned_data = array(
					'status' => 'sukses',
				);
			} else {
				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}
			echo json_encode($returned_data);
		} else show_404();
	}

	function ganti_email(){
		$this->headbar->add_header('Ganti Email');
		$this->init_sidebar();
		$this->main_content->add_custom('p','Mengganti email Akun');
		$this->main_content->add_block()
			->add_text_field('email','Email Baru')
			->set_form($this->uri->segment(1).'/ajax_ganti_email','ganti_email');
		$this->main_content->add_script('ganti_password');
		$this->load->view('template', array('title' => 'Configuration'));
	}

	function ajax_ganti_email(){
		if(IS_AJAX){
			$this->load->library('form_validation');
			if($this->form_validation->run('ganti_email') == TRUE){

				$new_email = $this->input->post('email');

				$akun = models\Current_user::get_user();
				$akun->set_email($new_email);
				$this->doctrine->em->flush();

				// sementara
				$this->session->set_userdata(array('username' => $new_email));

				$returned_data = array(
					'status' => 'sukses',
				);
			} else {
				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}
			echo json_encode($returned_data);
		} else show_404();
	}

	//function log(){
	//	$this->headbar->add_header('Log');
	//	$this->init_sidebar();
	//	$this->main_content->add_custom('p','Daftar user yang login');
	//	$this->load->view('template', array('title' => 'Log'));
	//}

	// function email(){
		// $this->headbar->add_header('Ganti Email Gateway');
		// $this->init_sidebar();
		// $this->main_content->add_custom('p','Akun email yang digunakan untuk mengirim email');
		// $this->load->view('template', array('title' => 'Configuration'));
	// }

	//function edit_paket_speedy(){
	//	$this->headbar->add_header('Edit Paket Speedy');
	//	$this->init_sidebar();
	//	$this->main_content->add_custom('p','Paket speedy diatur disini');
	//	$this->load->view('template', array('title' => 'Configuration'));
	//}

	function reset_user_password(){
		$this->headbar->add_header('Reset User Password');
		$this->init_sidebar();
		$this->main_content->add_custom('p','Mengganti password dengan password default: abcde');
		$this->main_content->add_block()
			->add_password_field('password_admin','Password Admin')
			->add_text_field('id_akun','ID Akun')
			->set_form($this->uri->segment(1).'/ajax_reset_password','reset_password');
		$this->main_content->add_script('ganti_password');
		$this->load->view('template', array('title' => 'Configuration'));
	}

	function ajax_reset_password(){
		if(IS_AJAX){
			$this->load->library('form_validation');
			if($this->form_validation->run('reset_password') == TRUE){

				$id_akun = $this->input->post('id_akun');
				$akun= $this->doctrine->em->createQuery("
					SELECT a 
					FROM models\Akun a 
					WHERE a.username = '$id_akun'
					")->getResult();

				$akun = $akun[0];
				$akun->set_password('abcde');
				$this->doctrine->em->flush();

				$returned_data = array(
					'status' => 'sukses',
				);
			} else {
				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}
			echo json_encode($returned_data);
		} else show_404();
	}

	private function init_sidebar(){
		$this->sidebar->create_custom('p','Selamat Datang');
		$this->sidebar->create_identitas($this->current_user);
		$this->sidebar->create_menu($this->sidebar_menu_links);
	}

	private function teknisi_dashboard(){

		$tanggal = new DateTime();
		$tanggal_str = $tanggal->format('Y-m-d');
		$teknisi_id = $this->current_user->get_id();

		$array_kunjungan_hari_ini = $this->doctrine->em->createQuery("
			SELECT k 
			FROM models\Kunjungan k 
			JOIN k.teknisi t
			WHERE k.tanggal LIKE '$tanggal_str%'
			AND t.id = '$teknisi_id'
			")->getResult();

		$array_kunjungan = $this->doctrine->em->createQuery("
			SELECT k 
			FROM models\Kunjungan k 
			JOIN k.teknisi t
			WHERE t.id = '$teknisi_id'
			")->getResult();

		$array_kunjungan_belum_terlapor = array();
		foreach($array_kunjungan as $k){
			if(count($k->get_checklists()) == 0)
				$array_kunjungan_belum_terlapor[] = $k;
		}

		if(count($array_kunjungan_hari_ini) > 0){

			$this->main_content->add_custom('h4','Kunjungan hari ini:');
			$fields = array('customer','alamat','checklists');
			$this->creator->create_daftar_kunjungan($array_kunjungan_hari_ini,$fields);

		} else $this->main_content->add_custom('p','Tidak ada kunjungan hari ini');

		if(count($array_kunjungan_belum_terlapor) > 0){

			$this->main_content->add_custom('h4','Kunjungan yang belum dilaporkan :');
			$fields = array('customer','alamat');
			$this->creator->create_daftar_kunjungan($array_kunjungan_belum_terlapor,$fields);

		} else $this->main_content->add_custom('p','Semua kunjungan telah dilaporkan');
	}

	private function admin_dashboard(){

		$tanggal = new DateTime();
		$tanggal_str = $tanggal->format('Y-m-d');

		$array_kunjungan_hari_ini= $this->doctrine->em->createQuery("
			SELECT k 
			FROM models\Kunjungan k 
			WHERE k.tanggal LIKE '$tanggal_str%'
			")->getResult();

		$array_kunjungan = $this->doctrine->em->createQuery("
			SELECT k 
			FROM models\Kunjungan k 
			")->getResult();

		$array_kunjungan_belum_terlapor = array();
		foreach($array_kunjungan  as $k){
			if(count($k->get_checklists()) == 0)
				$array_kunjungan_belum_terlapor[] = $k;
		}

		if(count($array_kunjungan_hari_ini) > 0){

			$this->main_content->add_custom('h4','Kunjungan hari ini:');
			$fields = array('teknisi','customer','checklists');
			$this->creator->create_daftar_kunjungan($array_kunjungan_hari_ini,$fields);

		} else $this->main_content->add_custom('p','Tidak ada kunjungan hari ini');

		if(count($array_kunjungan_belum_terlapor) > 0){

			$this->main_content->add_custom('h4','Kunjungan yang belum dilaporkan :');
			$fields = array('teknisi','customer','alamat');
			$this->creator->create_daftar_kunjungan($array_kunjungan_belum_terlapor,$fields);

		} else $this->main_content->add_custom('p','Semua kunjungan telah dilaporkan');
	}

	private function customer_dashboard(){

		$tanggal = new DateTime();
		$tanggal_str = $tanggal->format('Y-m-d');
		$customer_id = $this->current_user->get_id();

		$array_kunjungan_hari_ini= $this->doctrine->em->createQuery("
			SELECT k 
			FROM models\Kunjungan k 
			JOIN k.customer c
			WHERE k.tanggal LIKE '$tanggal_str%'
			AND c.id = '$customer_id'
			")->getResult();

		if(count($array_kunjungan_hari_ini) > 0){

			$kunjungan = $array_kunjungan_hari_ini[0];
			$teknisi = $kunjungan->get_teknisi();
			$this->main_content->add_custom('h4','Teknisi untuk kunjungan hari ini: '.$teknisi->get_nama());
		} else $this->main_content->add_custom('h4','Tidak ada kunjungan hari ini');

	}
}
